<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<template v-if="isLogin">
			<view class="body">
				<view class="map">
					<map style="width: 100%; height: 400rpx;" :polyline="polylines" scale="16"
						:latitude="params.latitude" :longitude="params.longitude" :markers="markers" :circles="circles">
					</map>
					<cover-view class="get-local-btn" @click="getLocal">
						<cover-image class="local-img" src="@/static/images/get_local_icon.png"></cover-image>
					</cover-view>	
				</view>
				<view class="form-wrap m-b2">
					<view class="form-item">
						<view class="label">地 址：</view>
						<view class="form-input">
							<view class="content">
								<textarea class="inp" auto-height disabled v-model="params.address" placeholder="输入详细地址"
									maxlength="-1"></textarea>
							</view>
						</view>
					</view>

					<!-- <view class="form-item" v-if="params.address">
						<view class="label">定位精度：</view>
						<view class="form-input">
							<view class="content">
								<view class="inp">{{params.accuracy}} （<text
										:class="'leavel_'+params.leavel_num">{{params.leavel}}</text>）</view>
							</view>
						</view>
					</view> -->

					<view class="form-item">
						<view class="label">选择生境类型：</view>
						<template v-if="params.envtype">
							<picker class="form-picker" mode="selector" :range="envtypes" @change="changeEnvtypes">
								<view class="picker">
									<input type="text" class="select-data t-l" v-model="params.envtype"
										placeholder="选择生境类型" disabled>
									<view class="icon">
										<view class="iconfont f-s68">&#xeb8a;</view>
									</view>
								</view>
							</picker>
						</template>
						<template v-else>
							<view class="form-input">
								<view class="content">
									<input type="text" class="inp" v-model="params.envtype" placeholder="输入生境类型" />
								</view>
							</view>
						</template>
					</view>
					
					<view class="form-item">
						<view class="label">防控面积：</view>
						<view class="form-input">
							<view class="content">
								<input type="digit" class="inp" v-model="params.fk_area" placeholder="输入防控面积" />
								<view class="link">亩</view> 
							</view>
						</view>
					</view>
					
					<view class="form-item">
						<view class="label">防控方式</view>
						<radio-group class="form-radio" @change="setFkType">
							<view class="radio-item">
								<label>
									<radio class="radio" value="1" :checked="params.fk_type==1" color="#f00"/><text>人工</text>
								</label>
							</view>	
							<view class="radio-item">
								<label>
									<radio class="radio" value="2" :checked="params.fk_type==2" color="#f00"/><text>无人机</text>
								</label>
							</view>			 
						</radio-group>			
					</view> 
					
					<!-- 人工防控 -->
					<template v-if="params.fk_type==1">
						<view class="form-item">
							<view class="label">饵剂用量：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.bait_count" placeholder="输入饵剂用量"/>
									<view class="link">公斤</view>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<view class="label">粉剂用量：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.powder_count" placeholder="输入粉剂用量" />
									<view class="link">公斤</view>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<view class="label">扑杀蚁巢数：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.kill_count" placeholder="输入扑杀蚁巢数"/>
									<view class="link">个</view>
								</view>
							</view>
						</view>
					</template> 
					
					<!-- 无人机防控 -->
					<template v-if="params.fk_type==2">
						<view class="form-item">
							<view class="label">作业架次：</view>
							<view class="form-input">
								<view class="content">
									<input type="number" class="inp" v-model="params.sortie" placeholder="输入作业架次"/> 
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<view class="label">饵剂数量：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.bait_count" placeholder="输入饵剂数量" />
									<view class="link">公斤</view>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<view class="label">飞手姓名：</view>
							<view class="form-input">
								<view class="content">
									<input type="text" class="inp" v-model="params.hands" placeholder="输入飞手姓名"/> 
								</view>
							</view>
						</view>
					</template> 					
				</view>
  
				<!-- 药剂照片 -->
				<view class="form-wrap m-b2">
					<view class="upload-pic-item">
						<view class="label">药剂照片</view>
						<view class="form-upload-pic">
							<template>
								<view class="img-item" v-for="(item,index) in params.files_1" :key="index">
									<image src="@/static/images/filter_bg.png" mode="widthFix">
										<!-- 占位 -->
									</image>
									<view class="img-wrap" @click="previewPic(index,1)">
										<image class="img" :src="item.status==1?$imgPath(item.file_path):item.url" mode="widthFix"></image>
									</view>
									<view class="del-icon" @click="delPic(index,1)">
										<view class="iconfont">&#xe74b;</view>
									</view>
									<view v-if="item.status==0" class="loading-img">
										<div class="loading-icon loader-18"></div>
									</view>  
								</view>
							</template>
							<view class="img-item add-btn" @click="uploadImage(1)">
								<image src="@/static/images/upload_pic.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>

				<!-- 现场拍照 -->
				<view class="form-wrap m-b2">
					<view class="upload-pic-item">
						<view class="label">现场拍照</view>
						<view class="form-upload-pic">
							<template>
								<view class="img-item" v-for="(item,index) in params.files_2" :key="index">
									<image src="@/static/images/filter_bg.png" mode="widthFix">
										<!-- 占位 -->
									</image>
									<view class="img-wrap" @click="previewPic(index,2)">
										<image class="img" :src="item.status==1?$imgPath(item.file_path):item.url" mode="widthFix"></image>
									</view>
									<view class="del-icon" @click="delPic(index,2)">
										<view class="iconfont">&#xe74b;</view>
									</view>
									<view v-if="item.status==0" class="loading-img">
										<div class="loading-icon loader-18"></div>
									</view>  
								</view>
							</template>
							<view class="img-item add-btn" @click="uploadImage(2)">
								<image src="@/static/images/upload_pic.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>

				<view class="form-wrap m-b2">
					<view class="form-textarea-item">
						<view class="label a-t">备注信息</view>
						<view class="form-textarea">
							<textarea class="inp-textarea" v-model="params.remark" placeholder="备注信息(选填),如地块名称等"
								maxlength="-1"></textarea>
						</view>
					</view>
				</view>

			</view>

			<!-- 底部 -->
			<view class="footer">
				<view class="fix-wrap bottom">
					<view class="btn" @click="submit">提交</view>
				</view>
			</view>


		</template>
	</view>
</template>

<script>
	import {
		dateFormat,
		uploadImage,
		previewImage,
		uploadToServe
	} from '@/common/util.js'
	import {
		gd_map_Key,
		gd_map_Key_web
	} from '@/config.js'
	export default {
		name: 'name',
		data() {
			return {
				isLogin: true,
				isIphoneX: getApp().globalData.isIphoneX,
				userInfo: uni.getStorageSync('userInfo'),
				isStop: true,
				envtypes: [],
				catch_local: uni.getStorageSync('local'),
				isSaveData:false,    // 是否缓存数据	
				prev_data:{},
				params: {
					pid:'',
					pro_name:'',
					fk_type:'1',   // 1 人工  2 无人机
					fid: '0',
					accuracy: '',
					envtype: ''||uni.getStorageSync('pro_fk_data').env_type,
					fk_area: '',
					sortie: '',
					hands: '',
					bait_count:'',
					powder_count:'',
					kill_count:'',
					speed: '',
					address: '',
					files: [],
					files_1: [],   // 药剂
					files_2: [],   // 现场
					remark: ''||uni.getStorageSync('pro_fk_data').remark, 
					latitude: '',
					longitude: '',
					altitude: '',
					prevlatitude: '',
					prevlongitude: '',
				},
				timer:null,
				market_id:1,
				markers: [],
				circles: [{
					latitude: '',
					longitude: '',
					color: "#ff00000A",
					fillColor: "#ff00003A",
					radius: "50",
				}],
				polylines: [{
					points: [],
					color: "#fcd535",
					width: 10
				}],
			}


		},
		async onLoad(params) { 
			 
			this.params.pro_name=params.pro_name;
			this.params.pid=params.pid;
			this.isLogin = await this.$loginState('check');
			uni.setNavigationBarTitle({
				title:params.pro_name
			})
			
			this.prev_data=uni.getStorageSync('prev_fk_'+this.params.pid);    // 上次未提交数据 
			
			if (this.isLogin) {
				this.getLocal();
				
				this.getEnvtype();
				
				if(this.prev_data){
					let _this=this;
					uni.showModal({
						content:'有未提交的作业，是否继续上一次作业',
						success(res) {
							if(res.confirm){
								// 人工防控
								_this.params.fk_type= _this.prev_data.fk_type;   // ·防控方式
								_this.params.fk_area= _this.prev_data.fk_area;   // ·防控面积
								_this.params.bait_count=_this.prev_data.bait_count; // 饵剂数量,
								_this.params.powder_count=_this.prev_data.powder_count;  // 粉剂药数量
								_this.params.kill_count=_this.prev_data.kill_count;  // 扑杀蚁巢数	
								_this.params.files_1=_this.prev_data.files_1;
								_this.params.files_2=_this.prev_data.files_2;
								_this.params.sortie= _this.prev_data.sortie;   // ·作业架次,
								_this.params.hands= _this.prev_data.hands;   //  飞手姓名,
								_this.params.fid=this.prev_data.fid;
							}else{
								uni.removeStorageSync('prev_fk_'+_this.params.pid); 
							}							
						}
					})
				} 

			}
		},
		async onShow() {
			
			let _this = this;
			// 10s获取1次位置
			this.timer = setInterval(() => { 
				_this.updateLocus();
			}, 10000)

		},
		onHide() {
			if(!this.isSaveData&&(this.params.fk_area>0||this.params.sortie||this.params.hands||this.params.bait_count>0||this.params.powder_count>0||this.params.kill_count>0||this.params.files_1.length>0||this.params.files_2.length>0)){
				uni.setStorage({
					key:'prev_fk_'+this.params.pid,
					data:{
						fk_type:this.params.fk_type,
						fk_area: this.params.fk_area,   // ·防控面积 
						bait_count:this.params.bait_count, // 饵剂数量,
						powder_count:this.params.powder_count,  // 粉剂药数量
						kill_count:this.params.kill_count,  // 扑杀蚁巢数	
						files_1:this.params.files_1,
						files_2:this.params.files_2,
						sortie: this.params.sortie,   // ·作业架次,
						hands: this.params.hands,   //  飞手姓名,
						fid:this.params.fid
					}
				})
			}
			clearInterval(this.timer);    // 关闭轮询位置定时器
		},
		onUnload() {
			if(!this.isSaveData&&(this.params.fk_area>0||this.params.sortie||this.params.hands||this.params.bait_count>0||this.params.powder_count>0||this.params.kill_count>0||this.params.files_1.length>0||this.params.files_2.length>0)){ 
				uni.setStorage({
					key:'prev_fk_'+this.params.pid,
					data:{
						fk_type:this.params.fk_type,
						fk_area: this.params.fk_area,   // ·防控面积 
						bait_count:this.params.bait_count, // 饵剂数量,
						powder_count:this.params.powder_count,  // 粉剂药数量
						kill_count:this.params.kill_count,  // 扑杀蚁巢数	
						files_1:this.params.files_1,
						files_2:this.params.files_2,
						sortie: this.params.sortie,   // ·作业架次,
						hands: this.params.hands,   //  飞手姓名,
						fid:this.params.fid
					}
				})
			}
			
			clearInterval(this.timer);    // 关闭轮询位置定时器
		},
		methods: { 

			// 获取当前位置
			getLocal: function() {
				let _this = this;
				uni.showLoading({
					title:"获取位置中..."
				})
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude:true, 
					accuracy:50,
					success: function(res) { 
						uni.hideLoading();
						_this.params.accuracy = res.accuracy;
						_this.params.latitude = res.latitude;
						_this.params.longitude = res.longitude;
						_this.params.speed = res.speed;
						_this.params.altitude = res.altitude;
						
						_this.circles[0].latitude=_this.params.latitude;
						_this.circles[0].longitude=_this.params.longitude; 
						
						_this.markers[0] = {
							id: 0,
							latitude: parseFloat(_this.params.latitude),
							longitude: parseFloat(_this.params.longitude),
							iconPath: '/static/images/my_local.png', 
						}
 

						_this.getCityInfo(res.latitude, res.longitude); // 根据坐标获取城市信息

						_this.addLocus();

						uni.setStorage({
							key: 'local',
							data: {
								latitude: _this.params.latitude,
								longitude: _this.params.longitude
							}
						}) 
					},
					fail() {
						uni.hideLoading();
					}
				});
			},
			
			// 更新轨迹点
			updateLocus: function() {
				let _this = this; 
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude:true,
					highAccuracyExpireTime:5000,
					accuracy:50,
					success: function(res) {	
						console.log('获取位置') 
						//  判断是否相同地址范围 设置位置偏差
						let difx = Math.abs(_this.params.prevlongitude - res.longitude);
						let dify = Math.abs(_this.params.prevlatitude - res.latitude);
						
						if(res.accuracy<=30){
							if ((_this.params.prevlongitude == '' || _this.params.prevlatitude == '') || (difx > 0.00005 && difx < 0.0035) || (dify > 0.00005 && dify < 0.0035)) {
								console.log('位置变化')
							  _this.params.accuracy = res.accuracy;
							  _this.params.latitude = res.latitude;
							  _this.params.longitude = res.longitude;
							  _this.params.speed = res.speed;
							  _this.params.altitude = res.altitude;
							  
							  _this.circles[0].latitude= _this.params.latitude;
							  _this.circles[0].longitude= _this.params.longitude;
							  
							  _this.markers[0] = {
							  	id: 0,
							  	latitude: parseFloat(res.latitude),
							  	longitude: parseFloat(res.longitude),
							  	iconPath: '/static/images/my_local.png', 
							  }					
								
								let local = {
									latitude: parseFloat(res.latitude),
									longitude: parseFloat(res.longitude),
								}
								_this.polylines[0].points.push(local);
								
							  _this.addLocus(); 
							}
						}else{
							console.log('无效点')
							clearInterval(_this.timer);
							setTimeout(()=>{
								// 10s获取1次位置
								_this.timer = setInterval(() => {
									_this.updateLocus();
								}, 10000)
							},500)
						}					
					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							icon:"none",
							title:'获取位置失败',
							mask:true,
							duration:2000,
							success() { 
							}
						})
					}
				});
			},
			
 
			
			//获取天气信息
			getWeatherInfo:async function(adcode) {  
				let res=await this.$api.yfk_api.getWeather({
					key:gd_map_Key_web,
					city:adcode
				})  
				if(res != false && res.data.info == "OK"){ 
					if (res.data.lives.length > 0) { 
						this.params.weather = res.data.lives[0].weather;
						this.params.temperature = res.data.lives[0].temperature;
						this.params.humidity = res.data.lives[0].humidity;
					}
				}
				  
			},
			
			// 获取城市信息
			getCityInfo:async function(lat,lng){ 
				let res=await this.$api.yfk_api.getCityInfo({
					key:gd_map_Key_web,
					location:lng+','+lat
				})       
				if(res.data.status==1){  
					this.params.address= res.data.regeocode.formatted_address;
					this.getWeatherInfo(res.data.regeocode.addressComponent.adcode);    // 根据adcode 获取天气信息
				}
			},

			 
			// 获取生境类型
			getEnvtype: async function() {
				let res = await this.$api.yfk_api.getEnvtype();
				if (res.data.ret == 0) {
					res.data.data.envtypes.push('其他')
					this.envtypes = res.data.data.envtypes;
					if(!uni.getStorageSync('pro_fk_data').env_type){
						this.params.envtype = res.data.data.envtypes[0];
					}					
				}
			},
			
			// 防控方式
			setFkType: function(e) {
				this.params.fk_type = e.detail.value;
				this.params.bait_count=''; // 饵剂数量,
				this.params.powder_count='';  // 粉剂药数量
				this.params.kill_count='';  // 扑杀蚁巢数	
				this.params.sortie= '';   // ·作业架次,
				this.params.hands= '';   //  飞手姓名,
			},

			// 上传图片
			uploadImage: function(type) {
				// 上传现场照片
				let _this = this;
				uploadImage({
					length: type == 1 ? _this.params.files_1.length : _this.params.files_2.length, 
					maxSize: 1,
					sourceType:type == 1?['camera','album']:['camera'],
					success(res) { 
						 
						if (type == 1) { 
							let params_data = {};
							params_data.url=res.tempFilePaths[0];
							params_data.status=0;
							_this.params.files_1.push(params_data);
							
							 
							uploadToServe({
								url: '/project/upload-file',
								filePath: res.tempFilePaths[0],
								name: 'photo',		
								formData: {
									'type': 1,
									'file_type': res.tempFilePaths[0].split('.')[1],
									'uid': _this.userInfo.id
								} 						
							}).then((up_res)=>{   
								if(up_res.ret==0){ 									
									params_data.file_name=up_res.data.file_name;
									params_data.file_path=up_res.data.file_path;
									params_data.file_type=up_res.data.file_type;
									params_data.status=up_res.data.status;
									params_data.type=up_res.data.type;									
								}
							})	
							 
						} else if (type == 2) {	
							let params_data = {};
							params_data.url=res.tempFilePaths[0];
							params_data.status=0;
							_this.params.files_2.push(params_data);
							
							 
							uploadToServe({
								url: '/project/upload-file',
								filePath: res.tempFilePaths[0],
								name: 'photo',		
								formData: {
									'type': 2,
									'file_type': res.tempFilePaths[0].split('.')[1],
									'uid': _this.userInfo.id
								} 						
							}).then((up_res)=>{  
								if(up_res.ret==0){  
									params_data.file_name=up_res.data.file_name;
									params_data.file_path=up_res.data.file_path;
									params_data.file_type=up_res.data.file_type;
									params_data.status=up_res.data.status;
									params_data.type=up_res.data.type;  
								}
							})	
						}
					}
				})
				
				 
			},

			// 预览图片
			previewPic: function(index,type) {
				let _this = this;
				let preview_data = [];
				if(type==1){
					this.params.files_1.forEach(item => {
						preview_data.push(item.url);
					})
				}else if(type==2){
					this.params.files_2.forEach(item => {
						preview_data.push(item.url);
					})
				}
				
				previewImage({
					data: preview_data,
					index: index
				})
			},

			// 删除图片
			delPic: async function(index,type) {
				if(type==1){
					let res=await this.$api.ycj_api.deletefile({
						file_path:this.params_1.files[index].file_path
					}) 
					if(res.data.ret==0){
						this.params_1.files.splice(index, 1);
					}  
				}else if(type==2){
					let res=await this.$api.ycj_api.deletefile({
						file_path:this.params_2.files[index].file_path
					}) 
					if(res.data.ret==0){
						this.params_2.files.splice(index, 1);
					}  
				}	 
			},  

			// 切换生境
			changeEnvtypes: function(e) {
				if (e.detail.value == this.envtypes.length - 1) {
					this.params.envtype = '';
				} else {
					this.params.envtype = this.envtypes[e.detail.value];
				}
			},

			// 提交数据
			submit: async function() {
				let _this = this;

				if (this.params.fk_area == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入防控面积',
						mask: true,
						duration: 1000
					})
					return
				}
				
				 
				// 人工防控
				if(this.params.fk_type==1){
					if (this.params.bait_count == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入饵剂用量',
							mask: true,
							duration: 1000
						})
						return
					}
					
					if (this.params.powder_count == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入粉剂用量',
							mask: true,
							duration: 1000
						})
						return
					}
					
					if (this.params.kill_count == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入扑杀蚁巢数',
							mask: true,
							duration: 1000
						})
						return
					}
				}
				
				// 无人机防控
				if(this.params.fk_type==2){
					if (this.params.sortie == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入作业架次',
							mask: true,
							duration: 1000
						})
						return
					}
					if (this.params.bait_count == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入用药数量',
							mask: true,
							duration: 1000
						})
						return
					}
					if (this.params.hands == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入飞手姓名',
							mask: true,
							duration: 1000
						})
						return
					}
				}

				if (this.params.files_1.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传药剂照片',
						mask: true,
						duration: 1000
					})
					return
				};
				
				for (let i in this.params.files_1) {
					if (this.params.files_1[i].status == 0) {
						uni.showToast({
							icon: 'none',
							title: '药剂照片还未上传完',
							mask: true,
							duration: 1000
						})
						return false;
					}
				}
				
				if (this.params.files_2.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传现场照片',
						mask: true,
						duration: 1000
					})
					return
				};
				
				for (let i in this.params.files_2) {
					if (this.params.files_2[i].status == 0) {
						uni.showToast({
							icon: 'none',
							title: '现场照片还未上传完',
							mask: true,
							duration: 1000
						})
						return false;
					}
				}
 
				this.params.files=this.params.files_1.concat(this.params.files_2);				
				
				let params={};
				if(this.params.fk_type==1){
					params={
						type: this.params.fk_type,
						files: JSON.stringify(this.params.files),
						p_id: this.params.pid,
						fid: this.params.fid,
						fk_area: this.params.fk_area,   // ·防控面积						 
						bait_count:this.params.bait_count, // 饵剂数量,
						powder_count:this.params.powder_count,  // 粉剂药数量
						kill_count:this.params.kill_count,  // 扑杀蚁巢数
						speed: this.params.speed,
						accuracy: this.params.accuracy,
						gcj02_lat: this.params.latitude,
						gcj02_lng: this.params.longitude,
						address: this.params.address,
						env_type: this.params.envtype, 
						create_uid: this.userInfo.id,
						weather: this.params.weather,
						temperature: this.params.temperature,
						humidity: this.params.humidity,
						altitude: this.params.altitude,
						remark: this.params.remark,
					}
					
				}else if(this.params.fk_type==2){
					params={
						type: this.params.fk_type,
						files: JSON.stringify(this.params.files),
						p_id: this.params.pid,
						fid: this.params.fid,
						fk_area: this.params.fk_area,   // ·防控面积
						sortie: this.params.sortie,   // ·作业架次,
						hands: this.params.hands,   //  飞手姓名,
						bait_count:this.params.bait_count, // 饵剂数量, 
						speed: this.params.speed,
						accuracy: this.params.accuracy,
						gcj02_lat: this.params.latitude,
						gcj02_lng: this.params.longitude,
						address: this.params.address,
						env_type: this.params.envtype, 
						create_uid: this.userInfo.id,
						weather: this.params.weather,
						temperature: this.params.temperature,
						humidity: this.params.humidity,
						altitude: this.params.altitude,
						remark: this.params.remark,
					}
				}
 
				if (this.isStop) {
					this.isStop = false;
					let res = await this.$api.ycj_api.addFkdata(params);
					
					uni.showLoading({
						title: "数据提交中...",
						mask: true
					})  
					if (res.data.ret == 0) {
						_this.updateCollectData();
						this.params.fid = 0;
						setTimeout(()=>{
							uni.showToast({
								icon:"success",
								title: "数据提交成功",
								mask: true,
								duration: 1000,
								success() {
									uni.hideLoading();
									_this.isSaveData=true;
									setTimeout(() => {
										_this.isStop = true;
										uni.navigateBack({
											delta:1
										})
									}, 1100)
									
									uni.removeStorageSync('prev_fk_'+_this.params.pid);  // 清除上次数据 
									
									_this.$store.commit('UPDATE_PRO_FK_INFO', 1);   // 添加成功更新项目详情   
									
									uni.setStorage({
										key:'pro_fk_data',
										data:{
											env_type: _this.params.envtype,
											remark: _this.params.remark
										}
									})						
								}
							})  
						},1000)
						
						
					} else {
						uni.hideLoading();
						uni.showToast({
							image: '../../static/images/error.png',
							title: res.data.message,
							mask: true,
							duration: 1000,
							success() {
								setTimeout(() => {
									_this.isStop = true;
								})
							}
						})
					}

				}
			},

			//上传防控轨迹信息
			addLocus: async function() {
				let _this = this;
				uni.setKeepScreenOn({
					keepScreenOn: true
				});
				let res = await this.$api.ycj_api.setfklocus({
					'uid': this.userInfo.id,
					'pid': this.params.pid,
					'fid': this.params.fid,
					'speed': this.params.speed,
					'accuracy': this.params.accuracy,
					'gcj02_lat': this.params.latitude,
					'gcj02_lng': this.params.longitude,
				}); 
				 
				if (res.data.ret == 0) {
					this.params.prevlatitude = this.params.latitude;
					this.params.prevlongitude = this.params.longitude;
					
					if (this.params.fid == 0 && res.data.data > 0 && !this.prev_data.fid) {
						this.params.fid = res.data.data;
					}
					 
				}
			},

		 
			// 更新数据
			updateCollectData: async function() {
				let _this = this;
				let updateState=0;
				 
								
				// 上传用户作业点
				let set_area = await this.$api.ycj_api.addUserFkProLocus({
					pid: this.params.pid,
					uid: this.userInfo.id,
				}); 
				if(set_area.data.ret==0){
					updateState+=1;
				}else{ 
					uni.showToast({
						icon:'none',
						title: '轨迹点更新失败',
						mask: true,
						duration: 1000,
						success() { 
							setTimeout(() => { 
								_this.isStop = true;   
							}, 300)
						}
					}) 
				}
				
				//更新地区代码
				let area_code = await this.$api.ycj_api.adfkcode(); 
				if(area_code.data.ret==0){
					updateState+=1;
				}else{ 
					uni.showToast({
						icon:'none',
						title: '地区代码更新失败',
						mask: true,
						duration: 1000,
						success() { 
							setTimeout(() => { 
								_this.isStop = true;   
							}, 300)
						}
					}) 
				}

				//添加项目用户的监测点到mongodb
				setTimeout(async () => {
					let addMarkerUrl = await _this.$api.ycj_api.addMarkers({
						createUid: _this.userInfo.id,
						pid: _this.params.pid,
					}); 
					if(addMarkerUrl.data.ret==0){
						updateState+=1;
					}else{ 
						uni.showToast({
							icon:'none',
							title: '监测点更新失败',
							mask: true,
							duration: 1000,
							success() { 
								setTimeout(() => { 
									_this.isStop = true;   
								}, 300)
							}
						}) 
					}
					
					// if(updateState==3){
					// 	uni.hideLoading();
					// 	_this.isSaveData=true;
					// 	uni.showToast({
					// 		icon: 'success',
					// 		title: '数据更新成功',
					// 		mask: true,
					// 		duration: 1000,
					// 		success() {
					// 			setTimeout(() => {
					// 				_this.isStop = true;
					// 				uni.navigateBack({
					// 					delta:1
					// 				})
					// 			}, 1300)
								
					// 			uni.removeStorageSync('prev_fk_'+_this.params.pid);  // 清除上次数据 
								
					// 			_this.$store.commit('UPDATE_PRO_FK_INFO', 1);   // 添加成功更新项目详情   
								
					// 			uni.setStorage({
					// 				key:'pro_fk_data',
					// 				data:{
					// 					env_type: _this.params.envtype,
					// 					remark: _this.params.remark
					// 				}
					// 			})
					// 		}
					// 	})
					// }else{
					// 	uni.hideLoading();
					// 	uni.showToast({
					// 		icon: 'error',
					// 		title: '数据更新失败',
					// 		mask: true,
					// 		duration: 1000,
					// 		success() {
					// 			setTimeout(() => {
					// 				_this.isStop = true; 
					// 				uni.navigateBack({
					// 					delta:1
					// 				})
					// 			}, 300)								 
					// 		}
					// 	})
					// }
					
			 }, 800);
			},

		},
		components: {},

		computed: {

		},
		watch: {

		}

	}
</script>

<style lang="less" scoped>
	.form-wrap .label {
		width: 35%;
	}

	.form-wrap .content .link {
		color: #000;
	}

	.local-btn {
		position: absolute;
		right: 50rpx;
		top: 5rpx;
		padding: 3rpx 8rpx;
		background: #f00;
		border-radius: 5rpx;
		color: #fff;
		font-size: 30rpx;
	}

	.status {
		padding: 0 20rpx;
		color: #f00;
		font-size: 28rpx;
	}

	.hd-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.leavel_1 {
		color: #09f
	}

	.leavel_2 {
		color: #ff9600;
	}

	.leavel_3 {
		color: #f00;
	}

	.btn {
		@h: 80rpx;
		width: 98%;
		margin: 10rpx auto;
		text-align: center;
		height: @h;
		line-height: @h;
		background: linear-gradient(#e96b5c, #ce3c2f);
		border-radius: 10rpx;
		color: #fff;
		font-size: 34rpx;
	}

	.prev-btn {
		text-align: right;
		padding: 20rpx;

		text {
			padding: 4rpx 15rpx;
			background: #ff9600;
			border-radius: 8rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.prev-data {
		padding: 18rpx 10rpx;
		background: #f5f5f5;
		text-align: right;
		color: #999;
		font-size: 28rpx;

		.val {
			color: #333;
		}

		.use-btn {
			color: #d03c30;
		}
	}
</style>
